package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminSetmealController")
@RequestMapping("/admin/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private RedisTemplate redisTemplate;
    //新增套餐
    @PostMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public Result add(@RequestBody SetmealDTO setmealDTO) {
        setmealService.add(setmealDTO);
        return Result.success();
    }
    // 分页查询
    @GetMapping("/page")
    public Result<PageResult> page(SetmealPageQueryDTO setmeaPagelDTO) {
        PageResult pageResult = setmealService.pageQuery(setmeaPagelDTO);
        return Result.success(pageResult);
    }
    // 启用、禁用菜品
    @PostMapping("status/{status}")
    @CacheEvict(value = "setmealCache", allEntries = true)
    public Result<String> status(@PathVariable Integer status, Long id) {
        setmealService.updateStatus(status, id);
        return Result.success();
    }

    //根据ID查询套餐信息
    @GetMapping("/{id}")
    public Result<SetmealVO> getById(@PathVariable Long id) {
        SetmealVO setmealVO = setmealService.getById(id);
        return Result.success(setmealVO);
    }

    // 修改套餐信息
    @PutMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public Result<String> update(@RequestBody SetmealDTO setmealDTO) {
        setmealService.update(setmealDTO);
        return Result.success();
    }

    // 删除套餐
    @DeleteMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public Result<String> delete(@RequestParam List<Long> ids) {
        setmealService.delete(ids);
        return Result.success();
    }
    @RequestMapping("/list")
    public Result<List<SetmealVO>> list(@RequestParam Integer categoryId) {
        List<SetmealVO> list = setmealService.list(categoryId);
        return Result.success(list);
    }

    @RequestMapping("/dish/{id}")
    public Result<List<DishItemVO>> list(@PathVariable Long id) {
        List<DishItemVO> list = setmealService.dishItem(id);
        return Result.success(list);
    }
}
